package com.hontech.bread.ui.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.FrameLayout
import android.widget.TextView
import com.hontech.bread.R

class CargoState(context: Context, attrs: AttributeSet) : FrameLayout(context, attrs) {

    private val state: View
    private val tv: TextView

    init {
        val view = LayoutInflater.from(context).inflate(R.layout.view_cargo_state, this)

        state = view.findViewById(R.id.id_cargo_state_view)
        tv = view.findViewById(R.id.id_cargo_state_tv_key)

        val attr = context.obtainStyledAttributes(attrs, R.styleable.CargoState)
        val key = attr.getString(R.styleable.CargoState_cs_key)
        tv.text = key
        attr.recycle()
    }

    fun setKey(name: String) {
        tv.text = name
    }

    fun setCargoState(num: Int, pastDue: Boolean) {
        if (num == 0) {
            state.setBackgroundResource(R.drawable.point_gray)
        } else {
            if (pastDue) { // ${stringById(R.string.t1733460848_444)}/${stringById(R.string.t1733460848_445)} ${stringById(R.string.t1733460848_446)}
                state.setBackgroundResource(R.drawable.point_red2)
            } else {
                state.setBackgroundResource(R.drawable.point_green)
            }
        }
    }

    fun setState(ret: Boolean) {
        if (ret) {
            state.setBackgroundResource(R.drawable.point_red2)
        } else {
            state.setBackgroundResource(R.drawable.point_gray)
        }
    }

}